package com.scrx.ymg.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scrx.ymg.entity.OrderInform;
import com.scrx.ymg.entity.OrderToInform;
import com.scrx.ymg.entity.OrderToInformData;
import com.scrx.ymg.utilqy.HttpUtils;


public class CodeUtil {
	
	//小程序码
	private final static String QRurl="https://api.weixin.qq.com/wxa/getwxacode?access_token=";
	 
	 public static String getminiqrQr(String sceneStr, String accessToken) {
	        RestTemplate rest = new RestTemplate();
	        InputStream inputStream = null;
	        OutputStream outputStream = null;
	        String path="";
	        try {
	            String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+accessToken;
	            Map<String,Object> param = new HashMap<>();
	            param.put("scene", sceneStr);
	            param.put("page", "pages/homepage/homepage");
	            param.put("width", 375);
	            param.put("auto_color", false);
	            Map<String,Object> line_color = new HashMap<>();
	            line_color.put("r", 0);
	            line_color.put("g", 0);
	            line_color.put("b", 0);
	            param.put("line_color", line_color);
	            //LOG.info("调用生成微信URL接口传参:" + param);
	            MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
	            HttpEntity requestEntity = new HttpEntity(param, headers);
	            ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);
	            //LOG.info("调用小程序生成微信永久小程序码URL接口返回结果:" + entity.getBody());
	            byte[] result = entity.getBody();
	            //System.out.println(Base64.encodeBase64String(result));
	            inputStream = new ByteArrayInputStream(result);
	             path =BaseUtil.QRCode+sceneStr+".png";
	            File file = new File(path);
	            if (!file.exists()){
	                file.createNewFile();
	            }
	            outputStream = new FileOutputStream(file);
	            int len = 0;
	            byte[] buf = new byte[1024];
	            while ((len = inputStream.read(buf, 0, 1024)) != -1) {
	                outputStream.write(buf, 0, len);
	            }
	            outputStream.flush();
	        } catch (Exception e) {
	        	e.printStackTrace();
	        	System.out.println("调用小程序生成微信永久小程序码URL接口异常");
	            //LOG.error("调用小程序生成微信永久小程序码URL接口异常",e);
	        } finally {
	            if(inputStream != null){
	                try {
	                    inputStream.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
	            }
	            if(outputStream != null){
	                try {
	                    outputStream.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
	            }
	        }
	        return path;    
	    }
	 
	 
	    
	    
	    

	    
	   
	

	
	

}
